package com.gudaomai.wuye.web;

import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.gudaomai.wuye.common.CommonException;
import com.gudaomai.wuye.entity.House;
import com.gudaomai.wuye.entity.Layout;
import com.gudaomai.wuye.entity.LayoutForm;
import com.gudaomai.wuye.entity.RetEntity;
import com.gudaomai.wuye.service.ILayoutServ;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zenghualu
 * @since 2017-09-01
 */
@RestController
@RequestMapping("/wuye/layout")
public class LayoutCtrl extends BaseController {

	@Autowired
	ILayoutServ layoutService;

//	@Autowired
//	Wrapper<Layout> wrapper;

//	@Autowired
//	LayoutMapper layoutMapper;

	@ModelAttribute("layoutForm")
	public LayoutForm populateLayouts() {

		LayoutForm layoutForm = new LayoutForm();
		List<Layout> layouts = new ArrayList<Layout>();

		for (int i = 0; i < 10; i++) {
			layouts.add(new Layout());
		}
		layoutForm.setLayouts(layouts);
		return layoutForm;
	}

	@RequestMapping(method = RequestMethod.POST)
	public String saveForm(@ModelAttribute("layoutForm") LayoutForm layoutForm) throws CommonException {
		for (Layout layout : layoutForm.getLayouts()) {
			addLayout(layout);
		}

		return null;

	}

	/**
	 * Save an new layout
	 * 
	 * 1. Check if all required field are filled in. 2. Check if all the inputs
	 * are valid. 3. Check if the layout with the same properties does not exist
	 * in the DB. 4. Save the inputed values.
	 * 
	 * @param
	 * @author jess return
	 *
	 **/
	@ApiOperation("The layout settings page, click the add button, add commRegion")
	@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Layout addLayout(@RequestBody Layout layout) throws CommonException {

		Layout layoutEntity = null;
		
			// Create new layout entity
			layoutEntity = new Layout();

			// copy all user inputs to the new layout entity
			BeanUtils.copyProperties(layout, layoutEntity);

			// Insert the layout entity into the database
			layoutService.insert(layoutEntity);

		return layoutEntity;
	}

	/**
	 * update the existing layout
	 * 
	 * 1. Check if all required field are filled in. 2. Check if all the inputs
	 * are valid. 3. update the layout in the DB.
	 * 
	 * @param
	 * @author jess return
	 *
	 **/

	@ApiOperation("The layout settings page, click the modify button, update layout")
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	public Layout updateLayout(@RequestBody Layout layoutInput) throws Exception {

		RetEntity entity = new RetEntity();

		// update the layout entity in the database
		// Create new layout entity
		Layout layoutEntity = new Layout();
		// copy all user inputs to the new layout entity
		BeanUtils.copyProperties(layoutInput, layoutEntity);

		layoutService.updateById(layoutEntity);

		return layoutEntity;
	}

	/**
	 * delete the existing layout 1. Check if the corresponding Id exists in the
	 * DB. 2. Delete the layout from the DB.
	 * 
	 * @param id
	 * @author jess return
	 *
	 **/
	@ApiOperation("The layout settings page, click the delete button, delete layout")
	@RequestMapping(value = "/id/{id}", method = RequestMethod.DELETE)
	public RetEntity delLayoutById(@PathVariable("id") String id) throws Exception {

		RetEntity entity = new RetEntity();

		layoutService.deleteById(id);

		return entity;

	}

//	/**
//	 * generate the format
//	 * 
//	 * @param id
//	 * @author jess return
//	 *
//	 **/
//	@ApiOperation("The layout settings page, click the get format button, retrieve all layouts formatted")
//	@RequestMapping(value = "/", method = RequestMethod.GET)
//	public LayoutForm getAll() throws Exception {
//
//		List<Layout> layouts = layoutMapper.getAllLayouts();
//		LayoutForm layoutForm = new LayoutForm();
//
//		int x = layouts.size();
//
//		layoutForm.setLayouts(layouts);
//
//		return layoutForm;
//	}

	@ApiOperation("The layout settings page, click the get format button, retrieve all layouts formatted")
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public House getHouseByLayoutId(@PathVariable LayoutForm layoutForm) throws Exception {

		for (Layout layout : layoutForm.getLayouts()) {
			layout.getHouses();
		}
		return null;

	}

}
